package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IOnDelListener{
    private ListView stuListView;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> studentListView = new ArrayList<>();
    private StuAdapter adapter;
    private Button btnAddStu;
    private Button searchBut;
    private EditText searchEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stuListView = findViewById(R.id.lv_stu);
        searchBut = findViewById(R.id.searchBut);
        searchEdit = findViewById(R.id.searchEdit);

        initStuArrayList();
        adapter = new StuAdapter(MainActivity.this,
                studentArrayList, MainActivity.this );

        stuListView.setAdapter(adapter);

        stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent();
                intent.setClass(MainActivity.this,
                        EditActivity.class);
                intent.putExtra("NAME", studentArrayList.get(i).getName());
                intent.putExtra("AGE", studentArrayList.get(i).getAge());
                intent.putExtra("IMGID",studentArrayList.get(i).getImgId());
                intent.putExtra("INDEX", i);
                startActivityForResult(intent, 1002);
            }
        });

        btnAddStu = findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,
                        AddActivity.class);
                startActivityForResult(intent, 1001);
            }
        });

        searchBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchList();
            }
        });

        searchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                searchList();
            }
        });
    }

    private void searchList() {
        String edit = searchEdit.getText().toString();
        studentListView = new ArrayList<>();
        for (Student  students: studentArrayList){
            if(students.getName().contains(edit)){
                studentListView.add(students);
            }
        }
        adapter.setStudentArrayList(studentListView);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("MainActivity",
                "requestCode = " + requestCode + " resultCode = " + resultCode);

        if(data == null){
            return;
        }

        if(resultCode == 2001){
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.dog1);

            Student s = new Student(name, age, imgId);
            studentArrayList.add(s);
            adapter.notifyDataSetChanged();
        }else if(resultCode == 2002){
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int img = data.getIntExtra("IMG", R.drawable.dog1);
            int index = data.getIntExtra("INDEX", 0);

            studentArrayList.get(index).setName(name);
            studentArrayList.get(index).setAge(age);
            studentArrayList.get(index).setImgId(img);

            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 初始化学生列表的数据
     */
    private void initStuArrayList() {
        Student s1 = new Student("张一山", 18, R.drawable.dog1);
        Student s2 = new Student("王五", 19, R.drawable.dog2);
        Student s3 = new Student("赵六", 20, R.drawable.dog3);
        Student s4 = new Student("范冰冰", 21, R.drawable.dog4);
        Student s5 = new Student("迪丽热巴", 22, R.drawable.dog5);

        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);


    }

    @Override
    public void del(int i) {
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }
}
